package net.biancheng.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import net.biancheng.po.User;
@Component
public class UserConverter implements Converter<String, User> {
    @Override
    public User convert(String source) {
        // 创建User实例
        User user = new User();
        // 以“，”分隔
        System.out.println(source);
        String stringvalues[] = source.split(",");
        if (stringvalues != null && stringvalues.length == 3) {
            // 为user实例赋值
            user.setName(stringvalues[0].trim());
            user.setAge(Integer.parseInt(stringvalues[1].trim()));
            user.setHeight(Double.parseDouble(stringvalues[2].trim()));
            return user;
        } else {
            throw new IllegalArgumentException(String.format("类型转换失败， 需要格式'编程帮, 18,1.85',但格式是[% s ] ", source));
        }
    }
}